package com.fz.us.oa.web.controller.com;

import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.dict.entity.Dict;
import com.fz.us.admin.dict.entity.DictItem;
import com.fz.us.oa.core.service.com.PersonService;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dzw on 2018/3/15.
 */
@Controller
@RequestMapping(value = "/com/statistics")
public class StatisticsController extends BaseController {
    private static final long serialVersionUID = 8678437596882710303L;

    @Autowired
    private PersonService personService;

    //获取备选书记系统字典
    public DictItem beiXuan(){
        DictItem beixuan = null;
        Dict personNature = dictService.getByValue("personNature");
        if(personNature!=null){
            beixuan = dictItemService.getByValue(personNature.getId(), "shujihoubei");
        }
        return beixuan;
    }

    /**
     * 拼接柱状图map
     * @param xNameList x轴名称列表
     * @param yNumList 具体数值列表
     * @param titleText 标题
     * @return
     */
    public Map<String, Object> getBarMap(List<String> xNameList, List<Integer> yNumList, String titleText){
        Map<String, Object> option = new HashMap<String, Object>();
        option.put("color", new String[]{"#3398DB"});

        //鼠标移动时的效果
        Map<String, Object> tooltip = new HashMap<String, Object>();
        Map<String, Object> axisPointer = new HashMap<String, Object>();
        axisPointer.put("type", "shadow");
        tooltip.put("trigger", "axis");
        tooltip.put("axisPointer", axisPointer);
        option.put("tooltip", tooltip);

        //整体位置
        Map<String, Object> grid = new HashMap<String, Object>();
        grid.put("left", "3%");
        grid.put("right", "4%");
        grid.put("bottom", "3%");
        grid.put("containLabel", true);
        option.put("grid", grid);

        //x轴
        List<Map<String, Object>> xAxis = new ArrayList<Map<String, Object>>();
        Map<String, Object> xAxixData = new HashMap<String, Object>();
        xAxixData.put("type", "category");
        xAxixData.put("data", xNameList);
        Map<String, Object> axisTick = new HashMap<String, Object>();
        axisTick.put("alignWithLabel", true);
        xAxixData.put("axisTick", axisTick);
        xAxis.add(xAxixData);
        option.put("xAxis", xAxis);

        //y轴
        List<Map<String, Object>> yAxis = new ArrayList<Map<String, Object>>();
        Map<String, Object> yAxisData = new HashMap<String, Object>();
        yAxisData.put("type", "value");
        yAxis.add(yAxisData);
        option.put("yAxis", yAxis);

        //具体数值
        List<Map<String, Object>> series = new ArrayList<Map<String, Object>>();
        Map<String, Object> seriesData = new HashMap<String, Object>();
        seriesData.put("type", "bar");
        seriesData.put("barWidth", "60%");
        seriesData.put("data", yNumList);
        //柱状图上显示数值
        Map<String, Object> label = new HashMap<String, Object>();
        Map<String, Object> normal = new HashMap<String, Object>();
        normal.put("show", true);
        normal.put("position", "top");
        label.put("normal", normal);
        seriesData.put("label", label);
        series.add(seriesData);
        option.put("series", series);

        //标题
        Map<String, Object> title = new HashMap<String, Object>();
        title.put("text", titleText);
        title.put("left", "center");
        title.put("top", 10);
        option.put("title", title);

        return option;
    }

    /**
     * 拼接饼图map
     * @param xNameList 名字列表
     * @param pieDataList 具体数据map列表
     * @param titleText 标题
     * @return
     */
    public Map<String, Object> getPieMap(List<String> xNameList, List<Map<String, Object>> pieDataList, String titleText){
        Map<String, Object> option = new HashMap<String, Object>();

        Map<String, Object> title = new HashMap<String, Object>();
        title.put("text", titleText);
        title.put("x", "center");
        title.put("top", 10);
        option.put("title", title);

        Map<String, Object> tooltip = new HashMap<String, Object>();
        tooltip.put("trigger", "item");
        tooltip.put("formatter", "{a} <br/>{b} : {c} ({d}%)"); //json传大括号
        option.put("tooltip", tooltip);

        Map<String, Object> legend = new HashMap<String, Object>();
        legend.put("orient", "vertical");
        legend.put("left", "left");
        legend.put("data", xNameList);
        option.put("legend", legend);

        List<Map<String, Object>> series = new ArrayList<Map<String, Object>>();
        Map<String, Object> seriesDataMap = new HashMap<String, Object>();
        seriesDataMap.put("name", titleText);
        seriesDataMap.put("type", "pie");
        seriesDataMap.put("radius", "55%");
        seriesDataMap.put("center", new String[]{"50%","50%"});
        seriesDataMap.put("data", pieDataList);
        Map<String, Object> itemStyle = new HashMap<String, Object>();
        Map<String, Object> emphasis = new HashMap<String, Object>();
        emphasis.put("shadowBlur", 10);
        emphasis.put("shadowOffsetX", 0);
        emphasis.put("shadowColor", "'rgba(0, 0, 0, 0.5)");
        itemStyle.put("emphasis", emphasis);
        seriesDataMap.put("itemStyle", itemStyle);
        series.add(seriesDataMap);
        option.put("series", series);

        return option;
    }

    /**
     * 学历-全区 统计图 （streetId不为空的话就是 学历-街道）
     */
    @RequestMapping(value = "eduAll", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String eduAll(@CurrentUser User user, Model model, String streetId){
        DictItem generalAfter = getGeneralAfter();

        List<DictItem> eduList = getDictItems(user, "edu");
        DictItem street = null;
        String streetName = "";
        if(StringUtils.isNotEmpty(streetId)){
            street = dictItemService.get(streetId);
            streetName=street!=null ? street.getName() : "";
        }
        
        //柱状图数据
        List<String> xNameList = new ArrayList<String>();
        List<Integer> seriesDataList = new ArrayList<Integer>();

        //饼图数据
        List<Map<String, Object>> pieDataList = new ArrayList<Map<String, Object>>();
        Map<String, Object> pieDataMap;

        //居民区书记学历分布
        for(DictItem edu:eduList){
            xNameList.add(edu.getName()); //横坐标名字
            int count;
            if(StringUtils.isEmpty(streetId)){
                count = personService.getCountEduAll(false,beiXuan(),edu, generalAfter);
            }else{
                count = personService.getCountEduAll(false,beiXuan(),edu,street, generalAfter);
            }
            seriesDataList.add(count);

            pieDataMap = new HashMap<String, Object>();
            pieDataMap.put("value", count);
            pieDataMap.put("name", edu.getName());
            pieDataList.add(pieDataMap);
        }
        Map<String, Object> juMinBar = getBarMap(xNameList, seriesDataList, streetName+"居民区书记学历分布");
        Map<String, Object> juMinPie = getPieMap(xNameList, pieDataList, streetName+"居民区书记学历分布比例");
        model.addAttribute("juMinBar", JSONObject.fromObject(juMinBar));
        model.addAttribute("juMinPie", JSONObject.fromObject(juMinPie));

        //备选书记学历分布
        xNameList = new ArrayList<String>();
        seriesDataList = new ArrayList<Integer>();
        pieDataList = new ArrayList<Map<String, Object>>();
        for(DictItem edu:eduList){
            xNameList.add(edu.getName()); //横坐标名字
            int count;
            if(StringUtils.isEmpty(streetId)) {
                count = personService.getCountEduAll(true,beiXuan(),edu, generalAfter);
            }else{
                count = personService.getCountEduAll(true,beiXuan(),edu,street, generalAfter);
            }
            seriesDataList.add(count);

            pieDataMap = new HashMap<String, Object>();
            pieDataMap.put("value", count);
            pieDataMap.put("name", edu.getName());
            pieDataList.add(pieDataMap);
        }
        Map<String, Object> beiXuanBar = getBarMap(xNameList, seriesDataList, streetName+"备选书记学历分布");
        Map<String, Object> beiXuanPie = getPieMap(xNameList, pieDataList, streetName+"备选书记学历分布比例");
        model.addAttribute("beiXuanBar", JSONObject.fromObject(beiXuanBar));
        model.addAttribute("beiXuanPie", JSONObject.fromObject(beiXuanPie));

        //区学历分布
        xNameList = new ArrayList<String>();
        seriesDataList = new ArrayList<Integer>();
        pieDataList = new ArrayList<Map<String, Object>>();
        for(DictItem edu:eduList){
            xNameList.add(edu.getName()); //横坐标名字
            int count;
            if(StringUtils.isEmpty(streetId)) {
                count = personService.getCountEduAll(edu, generalAfter);
            }else{
                count = personService.getCountEduAll(edu,street, generalAfter);
            }
            seriesDataList.add(count);

            pieDataMap = new HashMap<String, Object>();
            pieDataMap.put("value", count);
            pieDataMap.put("name", edu.getName());
            pieDataList.add(pieDataMap);
        }
        Map<String, Object> quBar = getBarMap(xNameList, seriesDataList, streetName+"区学历分布");
        Map<String, Object> quPie = getPieMap(xNameList, pieDataList, streetName+"区学历分布比例");
        model.addAttribute("quBar", JSONObject.fromObject(quBar));
        model.addAttribute("quPie", JSONObject.fromObject(quPie));

        if(StringUtils.isNotEmpty(streetId)){
            return "/statistics/edu_street_stat";
        }else{
            return "/statistics/edu_all_stat";
        }
    }

    /**
     * 性别年龄-全区 统计图 （streetId不为空的话就是 性别年龄-街道） （方法名字是年龄 其实统计的是性别）
     */
    @RequestMapping(value = "ageAll", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String ageAll(@CurrentUser User user, Model model, String streetId){

        DictItem generalAfter = getGeneralAfter();
        DictItem beixuan = beiXuan();

        DictItem street = null;
        String streetName = "";
        if(StringUtils.isNotEmpty(streetId)){
            street = dictItemService.get(streetId);
            streetName = street!=null ? street.getName() : "";
        }

        //柱状图数据
        List<String> xNameList = new ArrayList<String>();
        List<Integer> yNumList = new ArrayList<Integer>();

        //饼图数据
        List<Map<String, Object>> pieDataList = new ArrayList<Map<String, Object>>();
        Map<String, Object> pieDataMap = new HashMap<String, Object>();

        //居民区书记
        int juMinNan;
        int juMinNv;
        if(StringUtils.isNotEmpty(streetId)){
            juMinNan = personService.getCountByGenderAndBeiXuan(false,beixuan,"男", street, generalAfter);
            juMinNv = personService.getCountByGenderAndBeiXuan(false, beixuan, "女", street, generalAfter);
        }else{
            juMinNan = personService.getCountByGenderAndBeiXuan(false,beixuan,"男", generalAfter);
            juMinNv = personService.getCountByGenderAndBeiXuan(false, beixuan, "女", generalAfter);
        }

        xNameList.add("男");
        xNameList.add("女");
        yNumList.add(juMinNan);
        yNumList.add(juMinNv);
        pieDataMap.put("value", juMinNan);
        pieDataMap.put("name", "男");
        pieDataList.add(pieDataMap);

        pieDataMap = new HashMap<String, Object>();
        pieDataMap.put("value", juMinNv);
        pieDataMap.put("name", "女");
        pieDataList.add(pieDataMap);

        Map<String, Object> juMinBar = getBarMap(xNameList, yNumList, streetName+"居民区书记性别分布");
        Map<String, Object> juMinPie = getPieMap(xNameList, pieDataList, streetName+"居民区书记性别分布比例");
        model.addAttribute("juMinBar", JSONObject.fromObject(juMinBar));
        model.addAttribute("juMinPie", JSONObject.fromObject(juMinPie));

        //备选书记
        xNameList.clear();
        pieDataList.clear();
        yNumList.clear();
        int beiXuanNan;
        int beiXuanNv;
        if(StringUtils.isNotEmpty(streetId)){
            beiXuanNan = personService.getCountByGenderAndBeiXuan(true,beixuan,"男", street, generalAfter);
            beiXuanNv = personService.getCountByGenderAndBeiXuan(true, beixuan, "女", street, generalAfter);
        }else{
            beiXuanNan = personService.getCountByGenderAndBeiXuan(true,beixuan,"男", generalAfter);
            beiXuanNv = personService.getCountByGenderAndBeiXuan(true, beixuan, "女", generalAfter);
        }

        xNameList.add("男");
        xNameList.add("女");
        yNumList.add(beiXuanNan);
        yNumList.add(beiXuanNv);
        pieDataMap = new HashMap<String, Object>();
        pieDataMap.put("value", beiXuanNan);
        pieDataMap.put("name", "男");
        pieDataList.add(pieDataMap);
        pieDataMap = new HashMap<String, Object>();
        pieDataMap.put("value", beiXuanNv);
        pieDataMap.put("name", "女");
        pieDataList.add(pieDataMap);

        Map<String, Object> beiXuanBar = getBarMap(xNameList, yNumList, streetName+"备选书记性别分布");
        Map<String, Object> beiXuanPie = getPieMap(xNameList, pieDataList, streetName+"备选书记性别分布比例");
        model.addAttribute("beiXuanBar", JSONObject.fromObject(beiXuanBar));
        model.addAttribute("beiXuanPie", JSONObject.fromObject(beiXuanPie));

        //小计
        xNameList.clear();
        pieDataList.clear();
        yNumList.clear();
        xNameList.add("男");
        xNameList.add("女");
        yNumList.add(beiXuanNan + juMinNan);
        yNumList.add(beiXuanNv + juMinNv);
        pieDataMap = new HashMap<String, Object>();
        pieDataMap.put("value", beiXuanNan + juMinNan);
        pieDataMap.put("name", "男");
        pieDataList.add(pieDataMap);
        pieDataMap = new HashMap<String, Object>();
        pieDataMap.put("value", beiXuanNv + juMinNv);
        pieDataMap.put("name", "女");
        pieDataList.add(pieDataMap);

        Map<String, Object> quBar = getBarMap(xNameList, yNumList, streetName+"区性别分布");
        Map<String, Object> quPie = getPieMap(xNameList, pieDataList, streetName+"区性别分布比例");
        model.addAttribute("quBar", JSONObject.fromObject(quBar));
        model.addAttribute("quPie", JSONObject.fromObject(quPie));

        if(StringUtils.isNotEmpty(streetId)){
            return "/statistics/age_street_stat";
        }else{
            return "/statistics/age_all_stat";
        }
    }

    /**
     * 人员性质-全区 统计图 （streetId不为空的话就是 人员性质-街道）
     */
    @RequestMapping(value = "natureAll", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String natureAll(@CurrentUser User user, Model model, String streetId){

        DictItem generalAfter = getGeneralAfter();

        List<DictItem> personNature = getDictItems(user, "personNature");
        DictItem street = null;
        String streetName = "";
        if(StringUtils.isNotEmpty(streetId)){
            street = dictItemService.get(streetId);
            streetName = street!=null ? street.getName() : "";
        }

        //柱状图数据
        List<String> xNameList = new ArrayList<String>();
        List<Integer> yNumList = new ArrayList<Integer>();

        //饼图数据
        List<Map<String, Object>> pieDataList = new ArrayList<Map<String, Object>>();
        Map<String, Object> pieDataMap;

        for(DictItem nature:personNature){
            pieDataMap = new HashMap<String, Object>();
            if(!"shujihoubei".equals(nature.getValue())){
                int count;
                if(StringUtils.isNotEmpty(streetId)){
                    count = personService.getCountByNatureAndStreet(nature, street, generalAfter);
                }else{
                    count = personService.getCountByPersonNature(nature, generalAfter);
                }
                
                xNameList.add(nature.getName());
                yNumList.add(count);

                pieDataMap.put("value", count);
                pieDataMap.put("name", nature.getName());
                pieDataList.add(pieDataMap);
            }
        }
        Map<String, Object> juMinBar = getBarMap(xNameList, yNumList, streetName+"居民区书记人员性质分布");
        Map<String, Object> juMinPie = getPieMap(xNameList, pieDataList, streetName+"居民区书记人员性质比例");
        model.addAttribute("juMinBar", JSONObject.fromObject(juMinBar));
        model.addAttribute("juMinPie", JSONObject.fromObject(juMinPie));

        if(StringUtils.isNotEmpty(streetId)){
            return "/statistics/nature_street_stat";
        }else{
            return "/statistics/nature_all_stat";
        }
        
    }

}
